home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / pcxbgi2.exe / SHOWMJB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  5.3 KB  |  160 lines

  1. /**************************************************************************
  2.  *   showmjb.c                                                            *
  3.  *   - sample code for reading BGI format images written by PCX2BGI.EXE   *
  4.  *     Takes .mjb file as command line arg and displays a full screen     *
  5.  *      or partial-screen image                                           *
  6.  *   - marty balash                                                       *
  7.  *   - 03/02/91                                                           *
  8.  *                                                                        *
  9.  *   If you find any of these utilities useful, mail $15.00 to:           *
  10.  *      Marty Balash                                                      *
  11.  *      2 Pinecrest Dr.                                                   *
  12.  *      Prospect CT 06712                                                 *
  13.  *                                                                        *
  14.  *   Add EGAVGA.OBJ to project/make file                                  *
  15.  **************************************************************************/
  16.  
  17. #include <graphics.h>
  18. #include <alloc.h>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <string.h>
  22. #include <io.h>
  23.   
  24. struct IMAGEHDR{
  25.   char id[8];                 /* id      - use this for verifying format */
  26.   unsigned size;              /* size    - bytes to malloc */
  27.   struct palettetype palette; /* palette - for image */
  28. }hdr;
  29.   
  30.  
  31. long fileexists();
  32.  
  33. main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.   long filelen;
  38.   char filename[50];
  39.  
  40.   if(argc==1){
  41.     printf("SHOWMJB.EXE - By Marty Balash\n");
  42.     printf("              Read a file created by PCX2BGI\n\n");
  43.     printf("*** ERROR: Must Specify .MJB Filename\n");
  44.     exit(255);
  45.   }
  46.   strcpy(filename,argv[1]);
  47.   strupr(filename);
  48.   if(argc==2){
  49.     if(strstr(filename,".MJB")==NULL)
  50.       strcat(filename,".MJB");
  51.     if((filelen=fileexists(filename))==0){
  52.       printf("ERROR: Can't find %s",filename);
  53.       exit(255);
  54.     }
  55.   }
  56.   if(!openegascreen()){
  57.     puts("Can't open EGA screen");
  58.     exit(255);
  59.   }
  60.   if(filelen>100000L) /* whole screen is at least this big */
  61.     readscreen(filename);
  62.   else
  63.     readimage(filename);
  64.   getch();
  65.   closegraph();
  66. }
  67.  
  68. /**************************************************************************
  69.  * readimage - arg is filename of graphics file to display                *
  70.  *              returns 1 if successful, 0 if not                         *
  71.  **************************************************************************/
  72. readimage(fn)
  73. char *fn;
  74. {
  75.   int filehandle;
  76.   char *buff;
  77.  
  78.   if((filehandle=open(fn,O_RDONLY|O_BINARY))==-1)   /* open graphics file */
  79.     return(0);
  80.   read(filehandle,&hdr,sizeof(hdr));   /* read size (to malloc) & palette */
  81.   setallpalette(&hdr.palette);        /* set palette to palette from file */
  82.   if((buff=malloc(hdr.size))==NULL) /* alloc mem - unsigned int from file */
  83.     return(0);
  84.   read(filehandle,buff,hdr.size);                    /* read in the image */
  85.   putimage(50,30,buff,COPY_PUT);           /* put the image on the screen */
  86.   close(filehandle);
  87.   free(buff);                                                 /* clean up */
  88.   return(1);
  89. }
  90.  
  91. /***************************************************************************
  92.  * readscreen -  param is filename of graphics file to display             *
  93.  *               Returns 1 if successful, 0 if not                         *
  94.  *               Renders images on non-visable page, then switches pages   *
  95.  *               Screen is rendered with 5 separate screen-width images    *
  96.  ***************************************************************************/
  97. readscreen(fn)
  98. char *fn;
  99. {
  100.   int filehandle,i;
  101.   char *buff;
  102.  
  103.   if((filehandle=open(fn,O_RDONLY|O_BINARY))==-1)   /* open graphics file */
  104.     return(0);
  105.   read(filehandle,&hdr,sizeof(hdr));   /* read size (to malloc) & palette */
  106.   puts("LOADING...");
  107.   setactivepage(1);                      /* render graphics on other page */
  108.   setallpalette(&hdr.palette);        /* set palette to palette from file */
  109.   if((buff=malloc(hdr.size))==NULL) /* alloc mem - unsigned int from file */
  110.     return(0);
  111.   for(i=0;i<350;i+=70){
  112.     read(filehandle,buff,hdr.size);    /* read 5 horizonal image "slices" */
  113.     putimage(0,i,buff,COPY_PUT);        /* put images on the (other) page */
  114.   }
  115.   free(buff);                                                 /* clean up */
  116.   close(filehandle);
  117.   setvisualpage(1);  /* image rendered on non-visual page, display it now */
  118.   return(1);
  119. }
  120.   
  121. openegascreen() /* open 640x350 16-color EGA screen */
  122. {
  123.   int driver = EGA;
  124.   int mode =   EGAHI;
  125.   int result;
  126.   
  127.   if (registerbgidriver(EGAVGA_driver)<0){
  128.     printf("ERROR: Graphics System\n");
  129.     return(0);
  130.   }
  131.   initgraph(&driver,&mode,"");
  132.   result=graphresult();
  133.   if (result!=grOk) {
  134.     printf("ERROR : %s\n",grapherrormsg(result));
  135.     return(0);
  136.   }
  137.   return(1);
  138. }
  139.   
  140. long fileexists(fn)
  141. char *fn;
  142. {
  143.   int f;
  144.   long len;
  145.   if((f=open(fn,O_RDONLY))!=-1){
  146.     len=filelength(f);
  147.     read(f,&hdr,sizeof(hdr));
  148.     if(strcmp("PCX2BGI",hdr.id)!=0){  /* should always be "PCX2BGI" */
  149.       printf("ERROR: Incorrect Format or File Corrupt\n");
  150.       close(f);
  151.       exit(255);
  152.     }
  153.     close(f);
  154.     return(len);
  155.   }
  156.   else
  157.     return(0);
  158. }
  159.  
  160.